<?php
/**
 * Easthv Zend Framework Project
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * ZFDebug class
 *
 * @category   Zend
 * @uses       ZFDebug_Controller_Plugin_Debug
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 East Hui Van Business Consulting Co., Ltd. (http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
class Zend_Easthv_Debug extends ZFDebug_Controller_Plugin_Debug
{

    /**
     * Contains options to change Debug Bar behavior
     */
	protected $_options = array(
		'plugins'           => array(
			'Variables' => null,
			'Time' => null,
			'Memory' => null),
		'z-index'           => 255,
		'jquery_path'       => 'http://code.jquery.com/jquery-latest.min.js',
		'image_path'        => null
	);

	/**
	 * Appends Debug Bar html output to the original page
	 * If log opened, html will be wrote as log files
	 * 
	 * @access protected
	 * 
	 * @param string $html
	 * @return void
	 */
	protected function _output($html)
	{
		$config = Zend_Registry::get('config');

		$response = $this->getResponse();

		if($config['system']['debug'] > 2) {
			$html = "<div id='ZFDebug_offset'></div>\n<div id='ZFDebug'>\n$html\n</div>\n</body>";
			$response->setBody(str_ireplace('</body>', $this->_headerOutput() . $html, $response->getBody()));
		}

        if($config['system']['log']['enable']){

            try {

                $path = APPLICATION_PATH . '/../public' . $config['system']['dir']['logs'];

                $date = gmdate('Ymd');
                $mainfile = $path . $date . '.html';
                $detaildir =  $path . $date . '/';
                Zend_Easthv_File::makeDir($detaildir);
                list($usec, $sec) = explode(" ", microtime());
                $detailfile = date('H_i_s_') . $usec . $sec . '.html';
                $detail = $detaildir . $detailfile;

                //$mainStream = @fopen($mainfile, 'a', false);
                
                $mainLogger = new Zend_Log();
                $mainWriter = new Zend_Log_Writer_Stream($mainfile);
                $format = '%timestamp% [%ip%] %priorityName% (%priority%): %message%' . PHP_EOL;
                $formatter = new Zend_Log_Formatter_Simple($format);
                $mainWriter->setFormatter($formatter);


                $mainLogger->setEventItem('ip',$this->_getIP()); 
                $mainLogger->addWriter($mainWriter);
                $mainLogger->info("<p><a href=\"$date/$detailfile\">See details</a></p>");


                $detailWriter = new Zend_Log_Writer_Stream($detail);
                $detailLogger = new Zend_Log();

                $format = '%message%';
                $formatter = new Zend_Log_Formatter_Simple($format);
                $detailWriter->setFormatter($formatter);
                $detailLogger->addWriter($detailWriter);
                $detailLogger->info(
                    '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Debug</title>'.
                    $this->_headerOutput().
                    '<style>pre{margin:10px;background:#F0F0F0;font-size:12px;word-wrap:break-word;word-break:break-all;}</style>'.
                    '</head><body>'.
                    '<h3>HTTP Request</h3>'.
                    '<pre>'.
                    print_r (apache_request_headers() , true ).
                    '</pre>'.
                    '<h3>Server Response</h3>'.
                    '<pre>'.
                    print_r (apache_response_headers()  , true ).
                    '</pre>'.
                    '<h3>$_GET</h3>'.
                    '<pre>'.
                    print_r ($_GET, true ).
                    '</pre>'.
                    '<h3>$_POST</h3>'.
                    '<pre>'.
                    print_r ($_POST, true ).
                    '</pre>'.
                    '<h3>$_COOKIE</h3>'.
                    '<pre>'.
                    print_r ($_COOKIE, true ).
                    '</pre>'.
                    '<h3>$_SERVER</h3>'.
                    '<pre>'.
                    print_r ($_SERVER, true ).
                    '</pre>'.
                    '<div id="ZFDebug_debug">'.
                    $html.
                    '</div></body></html>'
                );
                

            } catch (Exception $e){

            }

        }

    }

    /**
     * Detect IP user address
     *
     * @access protected
     *
     * @return string ip address
     */
    protected function _getIp()
    {
        $realip = NULL;
        if (isset($_SERVER)) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $realip = getenv( 'HTTP_X_FORWARDED_FOR');
            } else if (getenv('HTTP_CLIENT_IP')) {
                $realip = getenv('HTTP_CLIENT_IP');
            } else {
                $realip = getenv('REMOTE_ADDR');
            }
        }
        return $realip;

    }
}
